package com.taofuxn.admin.api.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.taofuxn.admin.api.interceptors.ContextPathHandlerInterceptor;
import com.taofuxn.admin.api.interceptors.LoginHandlerInterceptor;
import com.taofuxn.admin.common.util.DateTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;
    @Autowired
    private ContextPathHandlerInterceptor contextPathHandlerInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(contextPathHandlerInterceptor).addPathPatterns("/**");
        registry.addInterceptor(loginHandlerInterceptor).excludePathPatterns("/static/**",
                "/v2/api-docs",
                "/swagger**",
                "/**/*swagger*/**"
                ).addPathPatterns("/**");

    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
        super.addResourceHandlers(registry);
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        HttpMessageConverter httpMessageConverter = getFastJsonHttpMessageConverter();
        converters.add(httpMessageConverter);
        converters.add(responseBodyConverter());
    }


    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        List<MediaType> supportedMediaTypes = Lists.newArrayList(Lists.newArrayList(MediaType.TEXT_XML, MediaType.TEXT_PLAIN));
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converter.setSupportedMediaTypes(supportedMediaTypes);
        return converter;
    }


    private FastJsonHttpMessageConverter getFastJsonHttpMessageConverter() {
        List<MediaType> supportedMediaTypes = Lists.newArrayList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_JSON);
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat(DateTools.LONG_DATE_FORMAT);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return fastJsonHttpMessageConverter;
    }
}
